<?php
/**
* @file rss_reader.php
* Systemblock RSS Feed Reader
*
* @author PortaMx - Portal Management Extension
* @author Copyright 2008-2011 by PortaMx - http://portamx.com
* @Version: 1.0 RC4
* @Date: 23.02.2011
*/

if(!defined('PortaMx'))
	die('This file can\'t be run without PortaMx');

/**
* @class pmxc_rss_reader
* Systemblock RSS Feed Reader
* @see rss_reader.php
* @author Copyright by PortaMx - http://portamx.com
*/
class pmxc_rss_reader extends PortaMxC_SystemBlock
{
	var $TimeToLife;		///< Feed TTL if send
	var $feedheader;		///< header info
	var $rsscontent;		///< content info

	/**
	* checkCacheStatus.
	* do nothing
	*/
	function pmxc_checkCacheStatus()
	{
		return true;
	}

	/**
	* InitContent.
	* Checks the cache status and create the content.
	*/
	function pmxc_InitContent()
	{
		global $context, $pmxCacheFunc;

		if($this->visible)
		{
			if($this->cfg['cache'] > 0)
			{
				// check if the block cached
				if(($cachedata = $pmxCacheFunc['get']($this->cache_key, $this->cache_mode)) !== null)
					list($this->feedheader, $this->rsscontent) = $cachedata;
				else
				{
					$this->rssreader_Content();
					$cachedata = array($this->feedheader, $this->rsscontent);
					$pmxCacheFunc['put']($this->cache_key, $cachedata, $this->TimeToLife, $this->cache_mode);
				}

				unset($cachedata);
			}
			$this->rssreader_Content();

			// paging...
			if(!empty($this->cfg['config']['settings']['onpage']) && count($this->rsscontent) > $this->cfg['config']['settings']['onpage'])
				$this->pmxc_constructPageIndex(count($this->rsscontent), $this->cfg['config']['settings']['onpage']);
		}

		// return the visibility flag (true/false)
		return $this->visible;
	}

	/**
	* rssreader_Content.
	* Prepare the content and save in $this->rsscontent.
	*/
	function rssreader_Content()
	{
		global $context;

		$this->TimeToLife = $this->cache_time;
		$this->rsscontent = '';
		$this->cfg['content'] = '';

		// get all articles from feed
		if(!empty($this->cfg['config']['settings']['rssfeedurl']))
		{
			// get All posts from feed
			$this->rsscontent = getRSSfeedPosts($this->feedheader, $this->cfg['config']['settings']['rssfeedurl'], $this->cfg['config']['settings']['rssmaxitems'], $this->cfg['config']['settings']['rsstimeout']);

			if(empty($this->feedheader['title']))
			{
				$this->feedheader['title'] = $this->cfg['config']['settings']['rssfeed_name'];
				$this->feedheader['link'] = $this->cfg['config']['settings']['rssfeed_link'];
				$this->feedheader['desc'] = $this->cfg['config']['settings']['rssfeed_desc'];
			}
			// Time To Life send ?
			if(!empty($this->cfg['config']['settings']['usettl']) && !empty($this->feedheader['ttl']))
				$this->TimeToLife = intval($this->feedheader['ttl']) * 60;
		}
	}

	/**
	* ShowContent.
	* Output the content and add necessary javascript
	*/
	function pmxc_ShowContent()
	{
		global $context, $modSettings, $txt;

		if(!empty($this->rsscontent))
		{
			$L_R = empty($context['right_to_left']) ? 'left' : 'right';
			$R_L = empty($context['right_to_left']) ? 'right' : 'left';

			$is_Split = $this->cfg['config']['settings']['split'];
			$is_last = (!empty($this->pageindex) ? ($this->startpage + $this->postspage > count($this->rsscontent) ? count($this->rsscontent) - $this->startpage : $this->postspage) : count($this->rsscontent));

			if($is_Split)
				$half = ceil($is_last / 2);
			else
				$half = $is_last;

			// write out the content
			if(!empty($this->cfg['config']['settings']['showhead']))
			{
				echo '
				<div class="smalltext">
					'. (!empty($this->feedheader['link']) ? '<a href="'. $this->feedheader['link'] .'" target="_blank"><b>'. $this->feedheader['title'] .'</b></a>' : '<b>'. $this->feedheader['link'] .'</b>') .'
					'. (!empty($this->feedheader['desc']) ? '<br />'. $this->feedheader['desc'] : '');

				if(!empty($this->pageindex) && !empty($this->cfg['config']['settings']['pgidxtop']))
					echo '
					<hr />';

				echo '
				</div>';
			}

			if(!empty($this->cfg['customclass']))
				$isCustFrame = !empty($this->cfg['customclass']['postframe']);
			else
				$isCustFrame = false;
			$spanclass = $isCustFrame && !empty($this->cfg['config']['visuals']['postbody']) ? $this->cfg['config']['visuals']['postbody'] .' ' : '';

			if(!empty($this->pageindex))
				echo '
				<a name="top'. $this->cfg['uniID'] .'"></a>';

			if(!empty($this->pageindex) && !empty($this->cfg['config']['settings']['pgidxtop']))
				echo '
			<div class="smalltext pmx_pgidx_top">'. $txt['pages']. ': '. $this->pageindex . (!empty($modSettings['topbottomEnable']) ? $context['menu_separator'] . ' &nbsp;&nbsp;<a href="#bot'. $this->cfg['id'] .'"><strong>' . $txt['go_down'] . '</strong></a>' : '') .'</div>';

			echo '
				<table width="100%" cellpadding="0" cellspacing="0" border="0" style="table-layout:fixed;'. (!empty($this->cfg['config']['settings']['showhead']) && (empty($this->pageindex) || empty($this->cfg['config']['settings']['pgidxtop'])) ? ' padding-top:'. $context['pmx']['settings']['panelpad'] .'px;' : '') .'">
					<tr>
						<td'.($is_Split ? ' style="padding-'.$R_L.':'. ceil($context['pmx']['settings']['panelpad'] / 2) .'px; width:50%;"' : '').' valign="top">';

			reset($this->rsscontent);
			for($i = 0; $i < $this->startpage; $i++)
				each($this->rsscontent);

			while(!empty($is_last))
			{
				list($pid, $post) = each($this->rsscontent);
				if($is_Split && empty($half))
				{
					echo '
					</td>
					<td style="padding-'.$L_R.':'. ceil($context['pmx']['settings']['panelpad'] / 2) .'px; width:50%;" valign="top">';

					reset($this->rsscontent);
					for($i = -1; $i < $this->startpage; $i++)
						each($this->rsscontent);
					list($pid, $post) = each($this->rsscontent);
				}
				$half--;
				$is_last--;
				$add_line = '';

				if(empty($half) && !empty($this->pageindex))
					echo '
						<a name="bot'. $this->cfg['uniID'] .'"></a>';

				echo '
						<div class="'. str_replace('bg', '_bar', $this->cfg['config']['visuals']['postheader']) .' catbg_grid">
							<h4 class="posthead '. $this->cfg['config']['visuals']['postheader'] .' catbg_grid">
								<img class="grid_images" src="'. $context['pmx_imageurl'] .'rssfeed.gif" alt="*" title="" />
								<span class="posthead grid_titles">';

				if(!empty($post['tlink']) || !empty($post['slink']))
					echo '
								<a class="normaltext" href="'. (!empty($post['tlink']) ? $post['tlink'] : $post['slink']) .'" target="_blank" title="'. str_replace('"', '\"', $post['subject']) .'">'. $post['subject'] .'</a>';
				else
					echo $post['subject'];

				echo '
								</span>
							</h4>
						</div>
						<div'.(empty($half) || empty($is_last) ? '' :' style="margin-bottom:'. $context['pmx']['settings']['panelpad'] .'px;"') .'>';

				$postbody = trim($this->cfg['config']['visuals']['postbody'] .' '. $this->cfg['config']['visuals']['postframe']);
				if(!empty($postbody))
					echo '
							<div class="'. $postbody .'" style="padding:0 5px;">';

				if(!empty($this->cfg['config']['settings']['equal']) && !empty($this->cfg['config']['settings']['split']))
					echo '
							<div class="pmx_equalheight_'. $this->cfg['id'] .'">';

				if(!empty($post['poster']))
				{
					$add_line = '<br />';
					echo $txt['pmx_text_postby'];

					if(!empty($post['plink']))
						echo '<a href="'. $post['plink'] .'" target="_blank">'. $post['poster'] .'</a>';
					else
						echo $post['poster'];
				}

				if(!empty($post['date']))
				{
					$add_line = '<br />';
					if(empty($post['poster']))
						echo $txt['pmx_rssreader_postat'];
					else
						echo ', ';
					echo $post['date'];
				}

				if(!empty($post['board']) || !empty($post['category']))
					echo $add_line . (!empty($post['board']) ? $txt['pmx_text_board'] . (!empty($post['blink']) ? '<a href="'. $post['blink'] .'" target="_blank">'. $post['board'] .'</a>' : $post['board']) : $txt['pmx_text_category'] .$post['category']);

				echo '
							'. (!empty($add_line) ? '<hr />' : '') .'
							<div style="overflow:hidden;">';

				if(!empty($this->cfg['config']['settings']['cont_encode']) && !empty($post['contenc']))
				{
					if(!empty($this->cfg['config']['settings']['delimage']))
						$post['contenc'] = PortaMx_revoveLinks($post['contenc'], false, true);
					if(!empty($this->cfg['config']['settings']['teaser']))
						echo PortaMx_Tease_posts($post['contenc'], $this->cfg['config']['settings']['teaser']);
					else
						echo $post['contenc'];
				}
				else
				{
					if(!empty($this->cfg['config']['settings']['delimage']))
						$post['message'] = PortaMx_revoveLinks($post['message'], false, true);
					if(!empty($this->cfg['config']['settings']['teaser']))
						echo PortaMx_Tease_posts($post['message'], $this->cfg['config']['settings']['teaser'], '', false, !empty($this->cfg['config']['settings']['delimages']));
					else
						echo $post['message'];
				}

				echo '
							</div>';

				if(!empty($this->cfg['config']['settings']['equal']) && !empty($this->cfg['config']['settings']['split']))
					echo '
							</div>';

				if(!empty($post['slink']))
					echo '
							<div class="smalltext">
								<a href="'. $post['slink'] .'" target="_blank">'. $txt['pmx_text_readmore'] .'</a>
							</div>';

				if(!empty($this->cfg['config']['visuals']['postframe']))
				{
					if($this->cfg['config']['visuals']['postframe'] == 'roundframe' || $isCustFrame)
						echo '
							</div>
							<span class="'. $spanclass . ($isCustFrame ? $this->cfg['config']['visuals']['postframe'] .'_bot' : 'lowerframe') .'"><span></span></span>';
					else
						echo '
							</div>
							<span class="'. (!empty($this->cfg['config']['visuals']['postbody']) ? $this->cfg['config']['visuals']['postbody'] .' ' : '') .'botslice"><span></span></span>';
				}
				elseif(!empty($postbody))
					echo '
							</div>';

				echo '
						</div>';

				if($is_Split)
					next($this->rsscontent);
			}

			if($is_Split && $half == 0 && count($this->rsscontent) % 2 != 0)
				echo '
					<td style="padding-'.$L_R.':'. ceil($context['pmx']['settings']['panelpad'] / 2) .'px; width:50%;" valign="top">';

			echo '
						</td>
					</tr>
				</table>';

			if(!empty($this->pageindex))
				echo '
				<div class="smalltext pmx_pgidx_bot">'. $txt['pages']. ': '. $this->pageindex . (!empty($modSettings['topbottomEnable']) ? $context['menu_separator'] . ' &nbsp;&nbsp;<a href="#top'. $this->cfg['uniID'] .'"><strong>' . $txt['go_up'] . '</strong></a>' : '') .'</div>';

			$ret = true;
		}
		else
		{
			if(!empty($context['pmx']['feed_error_text']))
				echo $txt['error_occured'] .'<br />'. $context['pmx']['feed_error_text'];
			else
				echo $txt['pmx_rssreader_error'];
		}
	}
}
?>